package csu.big_article.controller;

import csu.big_article.pojo.Result;
import csu.big_article.util.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUpdateController {
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        //把文件的内容存储到本地目录
        String originalFilename =file.getOriginalFilename();
        //这里如果两个人上传的文件/图片的名字相同的话 文件会进行覆盖 使用uuid Universally Unique Identifier
        String filename = UUID.randomUUID() +"_"+originalFilename;


//        //存储文件至阿里云
//        String url = AliOssUtil.upload(filename,file.getInputStream());

        //存储文件至本地磁盘
        file.transferTo(new File("F:\\javaweb_project\\big_artical\\src\\main\\java\\csu\\big_article\\images\\" +filename));
        String url = "http://localhost:8080/java/csu/big_article/images" +filename;
//        //存储文件至本地磁盘
//        file.transferTo(new File("C:\\Users\\32407\\Desktop\\temp\\"+filename));
        return Result.success(url);
    }
}
